﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{
	import flash.display.MovieClip;
//	import flash.utils.getDefinitionByName;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	
	public class MovieLoader extends MovieClip {

		private var fin:Boolean = false;
		private var maxX:int = 600;
		private var maxY:int = 250;
		
		public function MovieLoader(){
			invisible_mc.addEventListener(MouseEvent.MOUSE_MOVE, moverBolas);
			this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, progresar);
			this.loaderInfo.addEventListener(Event.COMPLETE, reproducir);
			addEventListener(Event.ENTER_FRAME, refresco);
		}
		
		private function progresar(e:ProgressEvent):void{
			var cargado:int = e.bytesLoaded;
			var total:int = e.bytesTotal;
			var porcentaje:Number = cargado/total;
			
			loaded_txt.text = Math.ceil(cargado/1024)+"KB";
			total_txt.text = Math.ceil(total/1024)+"KB";
			setCompleted(porcentaje);
			if(porcentaje > 0.85){
				fin = true;
				invisible_mc.alpha = (porcentaje-0.85)*6.67 ;
			}
		}
		
		private function reproducir(e:Event):void{
			invisible_mc.removeEventListener(MouseEvent.MOUSE_MOVE, moverBolas);
			this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progresar);
			this.loaderInfo.removeEventListener(Event.COMPLETE, reproducir);
			removeEventListener(Event.ENTER_FRAME, refresco);
		}
		
		private function setCompleted(percentage:Number):void{
			porcentaje_txt.text = String(Math.ceil(percentage*100))+"%";
			porcentajeReflejo_txt.text = String(Math.ceil(percentage*100))+"%";
			relleno_mc.setCompleted(percentage);
			rellenoReflejo_mc.setCompleted(percentage);
		}
		
		private function refresco(e:Event):void{
			if(!fin){
				if(invisible_mc.alpha > 0){
					invisible_mc.alpha -= 0.01;
				}
			}
			
			bola_mc.x += (maxX - bola_mc.x)*0.1;
			bola_mc.y += (maxY - bola_mc.y)*0.1;
			
			bolaReflejo_mc.y = 385+ (385-bola_mc.y);
			bolaReflejo_mc.x = bola_mc.x;
			
		}
		
		private function moverBolas(e:MouseEvent):void{
			maxX = mouseX;
			maxY = mouseY;
			if(maxY > 385 - bola_mc.width/2){
				maxY = 385 - bola_mc.width/2;
			}
		}

	}
}